package com.antler.smt.portal.service.impl;

import com.antler.smt.entity.JobOrderEntity;
import com.antler.smt.entity.RecruitmentInfoEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.JobOrderEntityMapper;
import com.antler.smt.portal.mapper.RecruitmentInfoEntityMapper;
import com.antler.smt.portal.mapper.RecruitmentInfoMapper;
import com.antler.smt.request.portal.ClassificationRecruitmentReq;
import com.antler.smt.request.portal.RecruitmentInfoReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.PostRecruitmentService;
import com.antler.smt.utils.DateUtil;
import com.antler.smt.utils.ReturnResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static com.antler.smt.constant.JobOrderConstant.*;


/**
 *  发布招聘需求
 *
 * @author zhangyi
 * @date 2018/7/11 15:14
 */
@Service
public class PostRecruitmentServiceImpl implements PostRecruitmentService {


    @Autowired
    private RecruitmentInfoEntityMapper recruitmentInfoEntityMapper;

    @Autowired
    private RecruitmentInfoMapper recruitmentInfoMapper;

    @Autowired
    private JobOrderEntityMapper jobOrderEntityMapper;

    /**
     * 插入招聘需求 TODO
     *
     * @param req
     * @param memberId
     * @return
     */
    @Override
    public Result insertRecruitmentInfo(RecruitmentInfoReq req,Long memberId) {

        //创建招聘需求实体
        RecruitmentInfoEntity recruitmentInfoEntity = new RecruitmentInfoEntity();
        recruitmentInfoEntity.setJobClassify(req.getJobClassify());
        recruitmentInfoEntity.setJobTitle(req.getJobTitle());
        recruitmentInfoEntity.setWorkType(req.getWorkType());
        recruitmentInfoEntity.setProvincial(req.getProvincial());
        recruitmentInfoEntity.setCounties(req.getCounties());
        recruitmentInfoEntity.setCity(req.getCity());
        recruitmentInfoEntity.setWorkPlace(req.getWorkPlace());
        recruitmentInfoEntity.setWorkTime(req.getWorkTime());
        recruitmentInfoEntity.setWorkEndTime(req.getWorkEndTime());
        recruitmentInfoEntity.setWorkStartTime(req.getWorkStartTime());
        recruitmentInfoEntity.setNumberPeople(req.getRecruitmentCount());
        recruitmentInfoEntity.setSexPeople(String.valueOf(req.getSexRequirement()));

        recruitmentInfoEntity.setSexPeople(String.valueOf(req.getSexPeople()));
        recruitmentInfoEntity.setAgePeopleStart(req.getAgePeopleStart());
        recruitmentInfoEntity.setAgePeopleEnd(req.getAgePeopleEnd());
        recruitmentInfoEntity.setDegreePeople(Integer.parseInt(req.getAcademicRequirement()));
        recruitmentInfoEntity.setExperiencePeople(req.getExperienceRequirement());
        recruitmentInfoEntity.setMemberId(memberId);
        recruitmentInfoEntity.setSalary(req.getSalary());
        recruitmentInfoEntity.setIncluding(req.getIncluding());
        recruitmentInfoEntity.setJobDescription(req.getJobDescription());
        recruitmentInfoEntity.setCompanyProfile(req.getCompanyProfile());

        //初始化时间
        recruitmentInfoEntity.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));
        recruitmentInfoEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));

        Long keyId = recruitmentInfoMapper.insertRecruitment(recruitmentInfoEntity);

        if(keyId != 0) {
            //生成初级工作订单
            JobOrderEntity jobOrderEntity = new JobOrderEntity();
            jobOrderEntity.setOrderNo(String.valueOf(System.currentTimeMillis() + "" + memberId));
            jobOrderEntity.setRecruitmentId(keyId);
            //添加订单状态
            jobOrderEntity.setStatus(COMMENT_WAIT_AUDIT);
            jobOrderEntity.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));
            jobOrderEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));
            int i = jobOrderEntityMapper.insertSelective(jobOrderEntity);
            return ReturnResultUtil.getResult("插入数据成功！","插入数据失败",i);
        }else{
            return  new Result(ResultCodeEnum.APPEND_DATA_ERROR,"插入数据失败");
        }
    }

    /**
     * 更新点击量
     *
     * @param id
     * @return
     */
    @Override
    public Result updateClickCount(Long id) {
        int count = 0;
        //获取点击次数
        count  = recruitmentInfoMapper.selectClickes(id);
        //原子自增
        AtomicInteger integer = new AtomicInteger(count);
        //自增后更新
        int i = integer.incrementAndGet();
        //创建自增点击数实体
        RecruitmentInfoEntity recruitmentInfoEntity = new RecruitmentInfoEntity();
        recruitmentInfoEntity.setId(id);
        recruitmentInfoEntity.setClicks(i);
        int j = recruitmentInfoEntityMapper.updateByPrimaryKeySelective(recruitmentInfoEntity);
        return ReturnResultUtil.getResult("添加自增成功！","添加操作数失败！",j);
    }

    /**
     * 分类查询招聘需求(0 未提交待审核、1提交未审核 2 审核通过 3 审核不通过)
     *
     * @param memberId
     * @return
     */
    @Override
    public Result selectRecruitmentInfo(Long memberId,String status) {
        Result result = null;
        PageInfo recruitmentInfos = null;
        String[] split = status.split(",");
        List<Integer> list = new ArrayList();
        for(int i = 0; i < split.length; i++){
            list.add(Integer.parseInt(split[i]));
        }
        ClassificationRecruitmentReq req = new ClassificationRecruitmentReq();
        req.setMemberId(memberId);
        req.setStatus(list);
        Page<RecruitmentInfoEntity> recruitmentInfoEntities = recruitmentInfoMapper.classificationSelect(req);
        //设置申请人数
        for(int i = 0; i < recruitmentInfoEntities.size(); i++) {
            int applyCount = recruitmentInfoMapper.selectApplyCount(recruitmentInfoEntities.get(i).getId());
            recruitmentInfoEntities.get(i).setApplyCount(applyCount);
        }
        if(recruitmentInfoEntities != null){
            recruitmentInfos = new PageInfo(recruitmentInfoEntities);
            result = new Result(recruitmentInfos);
        }else{
            return new Result(ResultCodeEnum.APPEND_DATA_ERROR,"分类查询失败！");
        }
        return result;
    }

}
